var taskQueue = [
  {start: 'task1 start', end: 'task1 end'},
  {start: 'task2 start', end: 'task2 end'},
  {start: 'task3 start', end: 'task3 end'},
]

function beginWork() {
  const task = taskQueue.shift()
  console.log(task.start)
  console.log('doing something...')
  console.log(task.end)
}


function workLoop(deadline) {
  console.log('当前帧剩余时间:', deadline.timeRemaining())
  // 空闲时间，一直循环跑任务
  while(deadline.timeRemaining() > 0 && taskQueue.length) {
    beginWork()
  }
  // 空闲时间到达，重新排队，等待下次空闲时间继续我们的任务
  if (taskQueue.length > 0) {
    window.requestIdleCallback(workLoop)
  }
}

window.requestIdleCallback(workLoop)